import { getRandom } from '../tool';
// sakura.ts
export class Sakura {
    constructor(
        public x: number,
        public y: number,
        public s: number,
        public r: number,
        public fn: {
            x: (x: number, y: number) => number,
            y: (x: number, y: number) => number,
            r: (r: number) => number
        }
    ) {}

    draw(cxt: CanvasRenderingContext2D, img: HTMLImageElement) {
        cxt.save();
        cxt.translate(this.x, this.y);
        cxt.rotate(this.r);
        cxt.drawImage(img, 0, 0, 40 * this.s, 40 * this.s);
        cxt.restore();
    }

    update() {
        this.x = this.fn.x(this.x, this.y);
        this.y = this.fn.y(this.x, this.y);
        this.r = this.fn.r(this.r);

        if (
            this.x > window.innerWidth || this.x < 0 ||
            this.y > window.innerHeight || this.y < 0
        ) {
            if (Math.random() > 0.4) {
                this.x = getRandom('x');
                this.y = 0;
            } else {
                this.x = window.innerWidth;
                this.y = getRandom('y');
            }
            this.s = getRandom('s');
            this.r = getRandom('r');
        }
    }
}

export class SakuraList {
    list: Sakura[] = [];

    push(sakura: Sakura) {
        this.list.push(sakura);
    }

    update() {
        for (const sakura of this.list) {
            sakura.update();
        }
    }

    draw(cxt: CanvasRenderingContext2D, img: HTMLImageElement) {
        for (const sakura of this.list) {
            sakura.draw(cxt, img);
        }
    }
}